package weixin.app.wx.service.rest.controller;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import org.apache.commons.io.IOUtils;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Conversions;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wechat.base.snd.QRCode;
import wechat.base.snd.QRCodeManager;
import wechat.base.snd.WCServiceApp;
import weixin.app.WXServiceAppFactoryDemo;
import weixin.app.domain.WXCommonMessage;
import weixin.app.wx.service.rest.entities.ResultSet;
import weixin.app.wx.service.rest.entities.Signature;
import weixin.app.wx.service.rest.util.SignatureGenerator;

@RestController
@SuppressWarnings("all")
public class WxServiceController {
  @RequestMapping("/qrcode")
  public ResultSet<byte[]> getQRCode(@RequestParam(value = "scene_id") final String scene_id) {
    ResultSet<byte[]> _xblockexpression = null;
    {
      final ResultSet<byte[]> result = new ResultSet<byte[]>();
      WCServiceApp _serviceApp = WXServiceAppFactoryDemo.getServiceApp();
      QRCodeManager _qRCodeManager = new QRCodeManager(_serviceApp);
      final Procedure1<QRCodeManager> _function = (QRCodeManager it) -> {
        try {
          QRCode _qRCode = new QRCode(scene_id);
          final File file = it.genPermTicket(_qRCode).getTicket(scene_id);
          if ((file == null)) {
            result.setCode("4");
            result.setMessage("failure");
          } else {
            result.setCode("0");
            result.setMessage("success");
            final FileInputStream in = new FileInputStream(file);
            result.setData(IOUtils.toByteArray(in));
          }
        } catch (Throwable _e) {
          throw Exceptions.sneakyThrow(_e);
        }
      };
      ObjectExtensions.<QRCodeManager>operator_doubleArrow(_qRCodeManager, _function);
      _xblockexpression = result;
    }
    return _xblockexpression;
  }
  
  /**
   * common message: text, audio, video, image
   */
  @RequestMapping("/message")
  public ResultSet<String> sendCommonMessage(@RequestParam(value = "wcuser_ids[]") final List<String> wcuser_ids, @RequestParam(value = "content") final String content) {
    ResultSet<String> _xblockexpression = null;
    {
      final ResultSet<String> result = new ResultSet<String>();
      final Consumer<String> _function = (String it) -> {
        InputOutput.<String>println(("=============>wc user id: " + it));
      };
      wcuser_ids.forEach(_function);
      final ArrayList<String> users = CollectionLiterals.<String>newArrayList();
      users.addAll(wcuser_ids);
      WXCommonMessage.sendCommonTxtMessage(null, CollectionLiterals.<String>newArrayList(((String[])Conversions.unwrapArray(users, String.class))), content);
      result.setCode("0");
      result.setMessage("success");
      _xblockexpression = result;
    }
    return _xblockexpression;
  }
  
  @RequestMapping("/signature")
  public ResultSet<Signature> getSignature(@RequestParam(value = "url") final String url) {
    ResultSet<Signature> _xblockexpression = null;
    {
      final ResultSet<Signature> result = new ResultSet<Signature>();
      try {
        result.setData(SignatureGenerator.generate(url));
        result.setCode("0");
        result.setMessage("success");
      } catch (final Throwable _t) {
        if (_t instanceof Exception) {
          final Exception e = (Exception)_t;
          result.setCode("4");
          result.setMessage("failure");
        } else {
          throw Exceptions.sneakyThrow(_t);
        }
      }
      _xblockexpression = result;
    }
    return _xblockexpression;
  }
  
  @RequestMapping("/templatemessage")
  public ResultSet<String> sendTemplateMessage() {
    ResultSet<String> _xblockexpression = null;
    {
      final ResultSet<String> result = new ResultSet<String>();
      _xblockexpression = result;
    }
    return _xblockexpression;
  }
}
